home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Minimalist Clock 1.0.3 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-06  |  5.9 KB  |  283 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Minimalist Clock
  4.     version 1.0.3
  5.     
  6.     Written by: Paul Celestin
  7.     
  8.     This simple application does nothing but display a simple clock in
  9.     a rectangle on your display. You can move the clock around, and you
  10.     can also check the date by clicking once on the clock.
  11.     
  12.     941207 - 1.0.0 initial release
  13.     951215 - 1.0.2 updated for CW7
  14.     960704 - 1.0.3 updated for CW9
  15.  
  16. ---------------------------------------------------------------------- */
  17.  
  18.  
  19. /* ----------------------------------------------------------------------
  20. includes
  21. ---------------------------------------------------------------------- */
  22.  
  23. #include    "the_defines.h"
  24. #include    "the_globals.h"
  25. #include    "the_prototypes.h"
  26.  
  27. Boolean            gDone,
  28.                 gWNEImplemented,
  29.                 gInBackground,
  30.                 gDrawDate = FALSE;
  31. long            gTicksOld = 0, gTicksNew = 0;
  32. EventRecord        gTheEvent;
  33. MenuHandle        gAppleMenu,
  34.                 gFileMenu,
  35.                 gEditMenu;
  36. WindowPtr        gClock;
  37.  
  38. /* ----------------------------------------------------------------------
  39. main - here is where it all began...
  40. ---------------------------------------------------------------------- */
  41. void main()
  42. {
  43.     Initialize();
  44.     MainLoop();
  45.     ExitToShell();
  46. }
  47.  
  48. /* ----------------------------------------------------------------------
  49. Initialize
  50. ---------------------------------------------------------------------- */
  51. void Initialize()
  52. {
  53.     Handle    myMenuBar;
  54.     
  55.     InitGraf(&qd.thePort);
  56.     InitFonts();
  57.     FlushEvents(everyEvent, 0);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(0L);
  62.     InitCursor();
  63.  
  64.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  65.     if (myMenuBar == NIL)
  66.         SysBeep(1);
  67.     else
  68.         SetMenuBar(myMenuBar);
  69.     
  70.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  71.     if (gAppleMenu == NIL)
  72.         SysBeep(1);
  73.     else
  74.         AddResMenu(gAppleMenu,'DRVR');
  75.     
  76.     gFileMenu = GetMHandle(MENU_FILE_ID);
  77.     if (gFileMenu == NIL)
  78.         SysBeep(1);
  79.  
  80.     gEditMenu = GetMHandle(MENU_EDIT_ID);
  81.     if (gEditMenu == NIL)
  82.         SysBeep(1);
  83.     
  84.     DrawMenuBar();
  85. }
  86.  
  87. /* ----------------------------------------------------------------------
  88. MainLoop
  89. ---------------------------------------------------------------------- */
  90. void MainLoop()
  91. {
  92.     RgnHandle    cursorRgn;
  93.     Boolean        gotEvent;
  94.     
  95.     gDone = false;
  96.     gInBackground = false;
  97.     
  98.     cursorRgn = NewRgn();
  99.     
  100.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  101.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  102.     
  103.     CreateClock();
  104.     
  105.     while (gDone == false)
  106.     {
  107.         if (gWNEImplemented)
  108.         gotEvent =
  109.             WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  110.         else
  111.         {
  112.             SystemTask();
  113.             gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  114.         }
  115.         
  116.         if (gotEvent)
  117.             Do();
  118.         else
  119.             DoIdle();
  120.     }
  121. }
  122.  
  123. /* ----------------------------------------------------------------------
  124. Do
  125. ---------------------------------------------------------------------- */
  126. void Do()
  127. {
  128.     char    c;
  129.     
  130.     switch (gTheEvent.what)
  131.     {
  132.         case nullEvent:
  133.             DoIdle();
  134.             break;
  135.         case mouseDown:
  136.             DoMouseDown();
  137.             break;
  138.         case keyDown:
  139.         case autoKey:
  140.             c = gTheEvent.message & charCodeMask;
  141.             if ((gTheEvent.modifiers & cmdKey) != 0)
  142.             {
  143.                 DoMenu(MenuKey(c));
  144.             }
  145.             break;
  146.         case activateEvt:
  147.             break;
  148.         case updateEvt:
  149.             break;
  150.         case app4Evt:
  151.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  152.             {
  153.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  154.             }
  155.             else
  156.                 DoIdle();
  157.             break;
  158.     }
  159. }
  160.  
  161. /* ----------------------------------------------------------------------
  162. DoIdle
  163. ---------------------------------------------------------------------- */
  164. void DoIdle()
  165. {
  166.     gTicksNew = TickCount();
  167.     if ((gTicksNew - gTicksOld) > UPDATE_INTERVAL)
  168.     {
  169.         gTicksOld = gTicksNew;
  170.         UpdateClock();
  171.     }
  172. }
  173.  
  174. /* ----------------------------------------------------------------------
  175. DoUpdate
  176. ---------------------------------------------------------------------- */
  177. void DoUpdate(window)
  178. WindowPtr window;
  179. {
  180.     GrafPtr    savedPort;
  181.     
  182.     GetPort(&savedPort);
  183.     SetPort(window);
  184.     BeginUpdate(window);
  185.     UpdateClock();
  186.     EndUpdate(window);
  187.     SetPort(savedPort);
  188. }
  189.  
  190. /* ----------------------------------------------------------------------
  191. DoMouseDown
  192. ---------------------------------------------------------------------- */
  193. void DoMouseDown()
  194. {
  195.     WindowPtr    window;
  196.     short int    thePart;
  197.     long int    menuChoice, windSize, newSize;
  198.     Boolean        doZoom, doGoAway;
  199.     
  200.     thePart = FindWindow(gTheEvent.where,&window);
  201.     switch (thePart)
  202.     {
  203.         case inMenuBar:
  204.             menuChoice = MenuSelect(gTheEvent.where);
  205.             DoMenu(menuChoice);
  206.             break;
  207.         case inSysWindow:
  208.             SystemClick(&gTheEvent,window);
  209.             break;
  210.         case inContent:
  211.             if (window != FrontWindow())
  212.                 SelectWindow(window);
  213.             break;
  214.         case inGrow:
  215.             newSize = GrowWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  216.             SizeWindow(window,LoWord(newSize),HiWord(newSize),false);
  217.             DoUpdate(window);
  218.             break;
  219.         case inDrag:
  220.             gDrawDate = TRUE;
  221.             DragWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  222.             DoUpdate(window);
  223.             break;
  224.         case inZoomIn:
  225.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomIn))
  226.             {
  227.                 SizeWindow(window,200,200,false);
  228.                 DoUpdate(window);
  229.             }
  230.             break;
  231.         case inZoomOut:
  232.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomOut))
  233.             {
  234.                 short winHeight,winWidth;
  235.  
  236.                 winHeight = qd.screenBits.bounds.bottom - 43;
  237.                 winWidth = qd.screenBits.bounds.right - 5;
  238.                 SizeWindow(window,winWidth,winHeight,false);
  239.                 MoveWindow(window,2,LMGetMBarHeight() + 20,false);
  240.                 DoUpdate(window);
  241.             }
  242.             break;
  243.             break;
  244.         case inGoAway:
  245.             if (doGoAway = TrackGoAway(window,gTheEvent.where))
  246.                 CloseWindow(window);
  247.             break;
  248.         default:
  249.             break;
  250.     }
  251. }
  252.  
  253. /* ----------------------------------------------------------------------
  254. DoMenu
  255. ---------------------------------------------------------------------- */
  256. void DoMenu(menuChoice)
  257. long int menuChoice;
  258. {
  259.     int    theMenu;
  260.     int    theItem;
  261.     
  262.     if (menuChoice != 0)
  263.     {
  264.         theMenu = HiWord(menuChoice);
  265.         theItem = LoWord(menuChoice);
  266.         switch (theMenu)
  267.         {
  268.             case MENU_APPLE_ID:
  269.                 DoMenuApple(theItem);
  270.                 break;
  271.             case MENU_FILE_ID:
  272.                 DoMenuFile(theItem);
  273.                 break;
  274.             case MENU_EDIT_ID:
  275.                 DoMenuEdit(theItem);
  276.                 break;
  277.             default:
  278.                 break;
  279.         }
  280.         HiliteMenu(0);
  281.     }
  282. }
  283.